package com.ies.ieschool.client.module.academic.view.content;



import com.google.gwt.core.client.GWT;
import com.ies.ieschool.client.module.academic.view.data.CourseData;
import com.ies.ieschool.client.module.academic.view.dialog.CourseDialog;
import com.ies.ieschool.client.object.ContextAreaFactory;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ListGridComponent;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IconButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;

/**
 * <p>Title: ระบบการจัดการงานโรงเรียน </p>
 * <p>Description: View ในส่วนของ หลักสูตร
 * <p>Copyright: Copyright (c) 2554</p>
 * <p>Company: Intelligent Enterprise Software</p>
 * @author Sharif Densumite
 * @version IES 2012
 * @since 5-04-2012
 */

public class CourseView extends VLayout{
	
	private static final String DESCRIPTION = "Course";
	private static final int COURSE_WIDTH = 1140;
	private static final int COURSE_HEIGHT = 580;
	
	ListGrid lsgCouseList;
	
    public CourseView() {
    	
    	GWT.log("init CourseView()...", null);

		this.addStyleName("contextArea");
		this.setWidth("*");
		this.setHeight(COURSE_HEIGHT+10);

		lsgCouseList = new ListGrid(); 
    	lsgCouseList.setWidth(COURSE_WIDTH);
    	lsgCouseList.setHeight(COURSE_HEIGHT); 	
    	lsgCouseList.setAutoFetchData(true);
    	lsgCouseList.setShowFilterEditor(true);
    	lsgCouseList.setFilterByCell(true);
    	lsgCouseList.setFilterOnKeypress(true);  
    	lsgCouseList.setDataSource(CourseData.getInstance());  

    	//field ชื่อหลักสูตร
    	ListGridField lsgfCourseName = new ListGridField("courseName","ชื่อหลักสูตร");
    	lsgfCourseName.setCellAlign(Alignment.LEFT);
    	lsgfCourseName.setAlign(Alignment.CENTER);
    	//field ช่วงชั้น
    	ListGridField lsgfClassRange = new ListGridField("classRange","ช่วงชั้น");
    	lsgfClassRange.setCellAlign(Alignment.LEFT);
    	lsgfClassRange.setAlign(Alignment.CENTER);
    	
    	ComboBoxItem cbiClassRange = new ComboBoxItem();  
    	cbiClassRange.setShowTitle(false);
    	cbiClassRange.setType("comboBox");  
    	cbiClassRange.setValueMap("ประถมต้น", "ประถมปลาย", "มัธยมต้น");
    	
    	lsgfClassRange.setEditorType(cbiClassRange);
    	
    	//field เกณฑ์การวัดผล
    	ListGridField lsgfCourseCriterion = new ListGridField("criterion","เกณฑ์การวัดผล");
    	lsgfCourseCriterion.setCellAlign(Alignment.LEFT);
    	lsgfCourseCriterion.setAlign(Alignment.CENTER);
    	
    	lsgCouseList.setFields(lsgfCourseName,lsgfClassRange,lsgfCourseCriterion);
    	lsgCouseList.addRecordDoubleClickHandler(lsgCouseListDoubleClick);	
    	
    	ToolStrip gridEditControls = new ToolStrip();  
        gridEditControls.setWidth100();  
        gridEditControls.setHeight(24);         

          
        IconButton editButton = new IconButton();  
        editButton.setIcon("icons/general/add.png");  
        editButton.setTitle("เพิ่มหลักสูตร");  
        editButton.addClickHandler(new ClickHandler() {              
            @Override  
            public void onClick(ClickEvent event) {  
                lsgCouseList.startEditingNew();               
            }  
        });  
          
        IconButton deleteButton = new IconButton();  
        deleteButton.setIcon("icons/general/delete.png");  
        deleteButton.setTitle("ลบหลักสูตร");  
        deleteButton.addClickHandler(new ClickHandler() {  
              
            @Override  
            public void onClick(ClickEvent event) {  
            	lsgCouseList.removeSelectedData();  
                  
            }  
        });  
        
        IconButton saveButton = new IconButton();  
        saveButton.setIcon("icons/general/save.png");  
        saveButton.setTitle("บันทึกหลักสูตร");  
        saveButton.addClickHandler(new ClickHandler() {              
            @Override  
            public void onClick(ClickEvent event) {  
                lsgCouseList.startEditingNew();               
            }  
        });  
          
        gridEditControls.setMembers(editButton, deleteButton, saveButton);
    	
    	lsgCouseList.setGridComponents(new Object[] {  
    			gridEditControls,
    			ListGridComponent.FILTER_EDITOR,
    			ListGridComponent.HEADER,   
                ListGridComponent.BODY   
                  
        });  
    	
    	addMember(lsgCouseList);
    	
    }         
    public RecordDoubleClickHandler lsgCouseListDoubleClick = new RecordDoubleClickHandler() {
        @Override
        public void onRecordDoubleClick(RecordDoubleClickEvent event) {
        	  new CourseDialog(event) ;
        }
	};
	
	public static class Factory implements ContextAreaFactory {

		private String id;

		public Canvas create() {
			CourseView view = new CourseView();
			id = view.getID();

			GWT.log("CourseView.Factory.create()->view.getID() - " + id, null);
			return view;
		}

		public String getID() {
			return id;
		}

		public String getDescription() {
			return DESCRIPTION;
		}
	}
  
}




